package corewarsxxi.logic;

import java.io.Serializable;

/**
 * The Direct addressing mode of Core Wars
 *
 * @version 1.0
 * @see     corewarsxxi.logic.AMDirect
 * @author  Luís Fonseca, ei10139 @ FEUP
 */
public class AMDirect extends AddressingMode implements Serializable
{
    /** Simplefied Singleton that provides access to the public final static <code>AMDirect</code> instance.*/
    public static final AMDirect self = new AMDirect();

    /**
     * Protected constructor of AMDirect.
     */
    protected AMDirect() {}

    /**
     * Calculates the value according to the current state of the memory array.
     * @param value     the value.
     * @param aField    look into either A-Field of B-Field.
     * @return          the value from Cores memory.
     */
    public int getValue(int value, boolean aField)
    {
        Core c = Core.getInstance();
        return (c.currentWarrior.processes.peek().offset + value) % c.getSize();
    }

    /**
     * Overrides the default Object.toString() function.
     * @return Always returns "<code>$$</code>".
     */
    @Override
    public String toString()
    {
        return "$$";
    }

    /**
     * Makes use of the Singleton pattern and returns the public static object.
     * @return it<i>self</i>.
     */
    @Override
    public AddressingMode getSelf()
    {
        return AMDirect.self;
    }
}
